//
//  LHNewHomePageViewController.m
//  Enjoy_Music
//
//  Created by Bruce_Lin on 15/6/8.
//  Copyright (c) 2015年 Bruce. All rights reserved.
//

#import "LHNewHomePageViewController.h"
#import "LHNewDetailViewController.h"
#import "LHNewHomeTableViewCell.h"
#import "LHNewHomePageModel.h"
#import <UIImageView+WebCache.h>
#import <MBProgressHUD.h>
#import <MJRefresh.h>
#import "MyNavgationView.h"

#define KNewTableViewCell @"NewReuse"

@interface LHNewHomePageViewController ()<UITableViewDelegate, UITableViewDataSource, MBProgressHUDDelegate>
@property (nonatomic ,strong)UITableView *newTableView;
@property (nonatomic ,strong)NSMutableArray *mainArr;
@property (nonatomic ,strong)MBProgressHUD *hud;
@property (nonatomic ,strong)MyNavgationView *navi;
@end
@implementation LHNewHomePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置状态栏颜色
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    
    //创建导航栏
    [self creatNavigation];
    
    self.mainArr = [NSMutableArray array];
    
    [self.view addSubview:self.newTableView];
    
    //创建小菊花
    self.hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:self.hud];
    self.hud.mode = MBProgressHUDModeAnnularDeterminate;
    self.hud.delegate = self;
    self.hud.labelText = @"Loading";
    [self.hud showWhileExecuting:@selector(myProgressTask) onTarget:self withObject:nil animated:YES];
}

- (void)creatNavigation
{
    //隐藏系统导航栏
    self.navigationController.navigationBarHidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.navi = [[MyNavgationView alloc]initWithFrame:CGRectMake(0, 0, LHWidth, 64)];
    [self.view addSubview:_navi];
    [_navi creatTitle:@"新歌推荐"];
    
    //更新状态栏颜色
    [self setNeedsStatusBarAppearanceUpdate];
}


- (void)getdata
{
    [LHHttpHandler get:@"http://online.dongting.com/recomm/new_songs_more?page=1&size=30&app=ttpod&v=v7.9.1.2015050518&uid=&mid=iPhone5C&f=f320&s=s310&imsi=&hid=&splus=8.2&active=1&net=2&openudid=7a702b310d3c0910aca4228a673a841751162b63&idfa=E1922CE5-ECA3-494B-9B6A-B637D35AFDC2&utdid=VXEJsNE34hwDAGokQcEmljVB&alf=201200&bundle_id=com.ttpod.music" body:nil result:LHHttp success:^(id result) {

#ifdef IOS_DEBUG
        NSLog(@"%@",result);
#endif
        
        for (NSDictionary *tempDic in [result objectForKey:@"data"]) {
            
            LHNewHomePageModel *model = [[LHNewHomePageModel alloc]initWithDic:tempDic];
            [_mainArr addObject:model];
        }
        
        //请求数据后刷新视图
        [_newTableView reloadData];
        
    } failure:^(NSError *error) {

#ifdef IOS_DEBUG
        NSLog(@"请求失败");
#endif
    }];
}


/**
 *  newTableView懒加载
 *
 *  @return newTableView
 */
- (UITableView *)newTableView
{
    if (!_newTableView) {
        self.newTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, LHWidth, LHHeight - 64 - 49) style:UITableViewStylePlain];
        _newTableView.dataSource = self;
        _newTableView.delegate = self;
        _newTableView.backgroundColor = [UIColor blackColor];
        [_newTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        [_newTableView registerClass:[LHNewHomeTableViewCell class] forCellReuseIdentifier:KNewTableViewCell];
    }
    return _newTableView;
}

#pragma mark - newTableView协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.mainArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LHNewHomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:KNewTableViewCell];
    cell.nameLabel.text = [_mainArr[indexPath.row]title];

    //设置图片淡入动画
    [cell.pic sd_setImageWithURL:[NSURL URLWithString:[_mainArr[indexPath.row] pic]] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        cell.pic.alpha = 0.0;
        [UIView transitionWithView:cell.pic duration:3.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
            [cell.pic setImage:image];
            cell.pic.alpha = 1.0;
            } completion:NULL];
    }];
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return LHHeight / 2.5;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
#ifdef IOS_DEBUG
    NSLog(@"%d",indexPath.row);
#endif
    
    //取消CELL点击阴影
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    LHNewDetailViewController *vc = [[LHNewDetailViewController alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    vc.msg_id = [_mainArr[indexPath.row] msg_id];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)hudWasHidden:(MBProgressHUD *)hud
{
    [hud removeFromSuperview];
    hud = nil;
}

-(void) myProgressTask
{
    float progress = 0.0f;
    
    [self getdata];
    
    while (progress < 1.0f) {
        progress += 0.01f;
        _hud.progress = progress;
        usleep(10000);
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
